﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Strom.Core.Characters;

namespace Strom.Core.Network.Packets
{
    public class CreateCharacterRequestPacket : IPacket
    {
        public static byte ID = 4;

        public void Unpack(BaseClient client, Packet packet)
        {
            string name = packet.Reader.ReadString();
            
            if(!((BaseServer)client.Tag).Database.CharactersManager.CharacterExists(name))
            {
                ((BaseServer)client.Tag).Database.CharactersManager.CreateCharacter(client, name);
                new CreateCharacterRequestResponsePacket().Pack(client, true);
            }
            else
                new CreateCharacterRequestResponsePacket().Pack(client, false);
        }

        /// <summary>
        /// Demande de création d'un personnage
        /// </summary>
        /// <param name="client">client en cours</param>
        /// <param name="datas">[0] nom</param>
        public void Pack(BaseClient client, params object[] datas)
        {
            var pw = new PacketWriter();
            pw.Write((string)(datas[0]));
            client.Send(pw.CompilePacket(ID));
        }
    }
}
